package com.mediastory.spsserver.notice.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mediastory.spsserver.category.service.CategorySvc;
import com.mediastory.spsserver.notice.domain.Notice;
import com.mediastory.spsserver.notice.service.NoticeSvc;

/**
 * Notice Ctrl
 * @author Jaewon Choi
 *
 */

@Controller
@RequestMapping(value={"/notice"})
public class NoticeCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private NoticeSvc noticeSvc;
	
	@Autowired
	private CategorySvc categorySvc;

	
	/**
	 * 목록 보기
	 * */
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		String beginTime = ServletRequestUtils.getStringParameter(request, "beginTime");
		String endTime = ServletRequestUtils.getStringParameter(request, "endTime");
		String categoryIdx = ServletRequestUtils.getStringParameter(request, "categoryIdx");
		
		condition.put("beginTime", beginTime);
		condition.put("endTime", endTime);
		condition.put("categoryIdx", categoryIdx);
		
		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(noticeSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		//카테고리 목록
		resultMap.put("categoryList", categorySvc.findbyList(condition));
		
		return resultMap;
	}
	
	/**
	 * 상세보기
	 * */
	@RequestMapping(value={"/detail.do"})
	public Map<String, Object> detail(HttpServletRequest request, HttpSession session, @RequestParam(required=true, value="idx")  Integer idx) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("notice", noticeSvc.findby(idx));
		
		return resultMap;
	}

	/**
	 * 등록,수정 폼
	 * */
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> form(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();

		int idx = ServletRequestUtils.getIntParameter(request, "idx", 0);
		if(idx != 0){
			resultMap.put("notice", noticeSvc.findby(idx));
		}
		
		//카테고리 목록
		resultMap.put("categoryList", categorySvc.findbyList(condition));
		
		return resultMap;
	}
	
	/**
	 * 등록/수정
	 * */
	@RequestMapping(value={"/submit.do"}, method=RequestMethod.POST)
	public String submit(Notice notice, HttpServletRequest request, HttpSession session) throws Exception {
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();
		
		if(notice.getIdx() != null){
			notice.setModifiedId(userId);
			notice.setModifiedIp(ip);
			noticeSvc.modify(notice);
		}else{
			notice.setCreatedId(userId);
			notice.setCreatedIp(ip);
			noticeSvc.add(notice);
		}

		return "notice/result";
	}
	
	/**
	 * 삭제
	 * */
	@RequestMapping(value={"/remove.do"}, method=RequestMethod.POST)
	public String remove(HttpServletRequest request, HttpSession session) throws Exception {
		
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();
		
		int[] idx = ServletRequestUtils.getIntParameters(request, "idx");
		
		if(idx.length == 0){
			throw new Exception("[ERROR] not checked.");
		}
		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("modifiedId", userId);
		condition.put("modifiedIp", ip);
		condition.put("idx", idx);
		
		noticeSvc.remove(condition);
		return "notice/result";
	}
	
	/**
	 * 긴급공지 설정
	 * */
	@RequestMapping(value={"/setIsMain.do"}, method=RequestMethod.GET)
	public String setIsMain(@RequestParam(required=false, value="idx")  Integer idx, HttpServletRequest request, HttpSession session) throws Exception {
		noticeSvc.modifyIsMain(idx);
		return "notice/result";
	}

	/**
	 * 앱 사용 페이지 - 목록 보기
	 * */
	@RequestMapping(value={"/appList"})
	public Map<String, Object> appList(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		condition.put("isNow", "true");
		condition.put("isMain", "0");
		condition.put("status", 1);
		
		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(noticeSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}
	
	/**
	 * 앱 사용 페이지 - 리스트 데이터 페이지
	 * */
	@RequestMapping(value={"/appListData"})
	public Map<String, Object> appListData(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		condition.put("isNow", "true");
		condition.put("isMain", "0");
		condition.put("status", 1);
		
		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(noticeSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}
	
	/**
	 * 앱 사용 페이지 - 메인 공지
	 * */
	@RequestMapping(value={"/appMain.do"})
	public Map<String, Object> appMain(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("isMain", true);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("mainList", noticeSvc.findbyList(condition));
		
		return resultMap;
	}
	
	/**
	 * 앱 사용 페이지 - 롯데 테스트 목록 보기
	 * */
	@RequestMapping(value={"/appListLotte"})
	public Map<String, Object> appListLotte(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		condition.put("isNow", "true");
		condition.put("isMain", "0");
		condition.put("status", 1);
		
		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(noticeSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}

}